package com.atwisdom.star.controller;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.common.vo.JsonResult;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.customized.PurchaseBslService;
import com.atwisdom.star.core.bsl.customized.SalaryBslService;
import com.atwisdom.star.core.bsl.functionClass.statistics.StatisticsService;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.atwisdom.star.log.pojo.SysLog;
import com.atwisdom.star.log.service.SysLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * 
 * @Date: 2023-05-25 15:15
 */
@AllArgsConstructor
@RestController
@Slf4j
@RequestMapping("/sysLog")
public class TestController {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private PurchaseBslService purchaseBslService;
    @Autowired
    private SalaryBslService salaryBslService;
    private StatisticsService statisticsService;

    @PostMapping("/page")
    public ResultInfo<?> page(@RequestBody Map<String, Object> requestMap) throws BusinessException {
        IPage<SysLog> page = new Page<>(1, 10);
        LambdaQueryWrapper<SysLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysLog::getRequestIP, requestMap.get("requestIP"))
                .orderByDesc(SysLog::getId);
        sysLogService.page(page, wrapper);
        return JsonResult.success(page);
    }

    @PostMapping("/supplierPrice")
    public ResultInfo<?> supplierPrice(@RequestBody JSONObject data) throws BusinessException {
        // JSONObject result = purchaseBslService.querySupplierPrice(data);
        return JsonResult.success();
    }

    @PostMapping("/calculateSalary")
    public ResultInfo<?> calculateSalary(@RequestBody JSONObject data) throws BusinessException, IllegalAccessException, NoSuchMethodException {
        return JsonResult.success(salaryBslService.calculateSalary(data));
    }

    @PostMapping("/statistics")
    public ResultInfo<?> statistics(@RequestBody ActionData actionData) throws BusinessException, IllegalAccessException, NoSuchMethodException {
        return JsonResult.success(statisticsService.calculate(actionData));
    }

    @PostMapping("/feignTest")
    public ResultInfo<?> feignTest(@RequestBody JSONObject data) {
        log.info("hello");
        log.info("data = {}", data);
        return JsonResult.success("success");
    }
}